#include <iostream>
#include <pthread.h>
#include <vector>
#include <unistd.h>

pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;	// 条件变量

void *MasterCore(void *args)
{
	sleep(2);
	std::cout << "master 开始工作" << std::endl;
	std::string name = static_cast<char *>(args);
	while (true)
	{
		pthread_cond_signal(&gcond); // 唤醒其中一个队列首部的线程
		// pthread_cond_broadcast(&gcond); // 唤醒队列中所有的线程
		std::cout << "master 唤醒一个线程..." << name << std::endl;
		sleep(1);
	}
}

void startMaster(std::vector<pthread_t> *tids)
{
	pthread_t tid;
	int n = pthread_create(&tid, nullptr, MasterCore, (void *)"Master Thread");
	if (n == 0)
	{
		std::cout << "create master success" << std::endl;
	}

	tids->emplace_back(tid);
}

void *SlaverCore(void *args)
{
	std::string name = static_cast<char *>(args);
	while (true)
	{
		// 1、加锁
		pthread_mutex_lock(&gmutex);

		// 2. 一般条件变量是在加锁和解锁之间使用的
		pthread_cond_wait(&gcond, &gmutex); // gmutex:这个是，是用来被释放的[前一半]
		std::cout << "当前被叫醒的线程是: " << name << std::endl;

		// 3、解锁
		pthread_mutex_unlock(&gmutex);
	}
}

void startSlaver(std::vector<pthread_t> *tids, int threadnum = 4)
{
	for (int i = 0; i < threadnum; i++)
	{
		char *name = new char[64];
		snprintf(name, 64, "slaver-%d", i + 1);
		pthread_t tid;
		int n = pthread_create(&tid, nullptr, SlaverCore, name);
		if (n == 0)
		{
			std::cout << "create success: " << name << std::endl;
			tids->emplace_back(tid);
		}
	}
}

void waitthread(std::vector<pthread_t> &tids)
{
	for (auto &tid : tids)
	{
		pthread_join(tid, nullptr);
	}
}

int main()
{
	std::vector<pthread_t> tids;
	startMaster(&tids);
	startSlaver(&tids, 6);
	waitthread(tids);
	return 0;
}